<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="订单保存后自动审核" prop="autoApproveOrder">
        <el-radio-group v-model="formData.autoApproveOrder">
          <el-radio
            v-for="dict in getBoolDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING)"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="订单在审核状态下可直接修改" prop="allowEditApprovedOrder">
        <el-radio-group v-model="formData.allowEditApprovedOrder">
          <el-radio
            v-for="dict in getBoolDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING)"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="系统数据备份路径" prop="dataBackupPath">
        <el-input v-model="formData.dataBackupPath" placeholder="请输入系统数据备份路径" />
      </el-form-item>
      <el-form-item label="排产提醒默认到交期天数" prop="defaultScheduleWarningDays">
        <el-input v-model="formData.defaultScheduleWarningDays" placeholder="请输入排产提醒默认到交期天数" />
      </el-form-item>
      <el-form-item label="算料规则" prop="materialCalculateRule">
        <el-select v-model="formData.materialCalculateRule" placeholder="请选择算料规则">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.PAPER_CARD_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否按模切规格计算材料" prop="useDieCutSpecForMaterialCalc">
        <el-radio-group v-model="formData.useDieCutSpecForMaterialCalc">
          <el-radio
            v-for="dict in getBoolDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING)"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="供应商报价策略" prop="supplierQuotePolicy">
        <el-select v-model="formData.supplierQuotePolicy" placeholder="请选择供应商报价策略">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.BASEORDERCONFIG_SUPPLIER_OFFER)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="净板约宽最小值" prop="minBoardWidth">
        <el-input v-model="formData.minBoardWidth" placeholder="请输入净板约宽最小值" />
      </el-form-item>
      <el-form-item label="原料采购是否按供应商" prop="directSupplierRawMaterial">
        <el-radio-group v-model="formData.directSupplierRawMaterial">
          <el-radio
            v-for="dict in getBoolDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING)"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="辅料采购是否按供应商" prop="directSupplierAuxiliaryMaterial">
        <el-radio-group v-model="formData.directSupplierAuxiliaryMaterial">
          <el-radio
            v-for="dict in getBoolDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING)"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="纸板采购默认交货天数" prop="defaultPaperboardDeliveryDays">
        <el-input v-model="formData.defaultPaperboardDeliveryDays" placeholder="请输入纸板采购默认交货天数" />
      </el-form-item>
      <el-form-item label="采购单价为0不允许保存" prop="preventZeroPriceOrder">
        <el-radio-group v-model="formData.preventZeroPriceOrder">
          <el-radio
            v-for="dict in getBoolDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING)"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="客户报价策略" prop="customerQuotePolicy">
        <el-select v-model="formData.customerQuotePolicy" placeholder="请选择客户报价策略">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.BASEORDERCONFIG_SUPPLIER_OFFER)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="纸箱订单默认交货天数" prop="defaultDeliveryDaysBox">
        <el-input v-model="formData.defaultDeliveryDaysBox" placeholder="请输入纸箱订单默认交货天数" />
      </el-form-item>
      <el-form-item label="彩印订单默认交货天数" prop="defaultDeliveryDaysColorBox">
        <el-input v-model="formData.defaultDeliveryDaysColorBox" placeholder="请输入彩印订单默认交货天数" />
      </el-form-item>
      <el-form-item label="销售订单保存后自动审核" prop="autoApproveSalesOrder">
        <el-radio-group v-model="formData.autoApproveSalesOrder">
          <el-radio
            v-for="dict in getBoolDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING)"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="不入库可以开送货单" prop="allowNonStockDelivery">
        <el-radio-group v-model="formData.allowNonStockDelivery">
          <el-radio
            v-for="dict in getBoolDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING)"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="送货需按排程发货" prop="enableSchedulingDelivery">
        <el-radio-group v-model="formData.enableSchedulingDelivery">
          <el-radio
            v-for="dict in getBoolDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING)"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="送货单车量超容量提醒" prop="carOvercapacityWarning">
        <el-radio-group v-model="formData.carOvercapacityWarning">
          <el-radio
            v-for="dict in getBoolDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING)"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="系统月结日" prop="systemMonthEnd">
        <el-input v-model="formData.systemMonthEnd" placeholder="请输入系统月结日" />
      </el-form-item>
      <el-form-item label="原料入库限制" prop="rawMaterialInLimit">
        <el-select v-model="formData.rawMaterialInLimit" placeholder="请选择原料入库限制">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.BASEORDERCONFIG_MATERIAL_WARE_IN_LIMIT)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="成品入库限制" prop="productInLimit">
        <el-select v-model="formData.productInLimit" placeholder="请选择成品入库限制">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.BASEORDERCONFIG_PRODUCT_WARE_IN_LIMIT)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="成品送货提示" prop="productDeliveryHint">
        <el-select v-model="formData.productDeliveryHint" placeholder="请选择成品送货提示">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.BASEORDERCONFIG_PRODUCT_DELIVERY_ALERT)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="产品默认单位" prop="defaultProductUnit">
        <el-select v-model="formData.defaultProductUnit" placeholder="请选择产品默认单位">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.DEFAULT_UNIT)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="成品入库限制2" prop="productInLimit2">
        <el-input v-model="formData.productInLimit2" placeholder="请输入成品入库限制2" />
      </el-form-item>
      <el-form-item label="送货单保存后自动审核" prop="deliveryAutoAudit">
        <el-radio-group v-model="formData.deliveryAutoAudit">
          <el-radio
            v-for="dict in getBoolDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING)"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="成品管理模式" prop="productManagementMode">
        <el-select v-model="formData.productManagementMode" placeholder="请选择成品管理模式">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.BASEORDERCONFIG_PRODUCT_MANAGE_MODE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="成品送货方式" prop="productDeliveryMode">
        <el-select v-model="formData.productDeliveryMode" placeholder="请选择成品送货方式">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.BASEORDERCONFIG_PRODUCT_DELIVERY_MODE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="送货单自动领用库存或者生成成品" prop="deliveryAutoPickOrProduce">
        <el-radio-group v-model="formData.deliveryAutoPickOrProduce">
          <el-radio
            v-for="dict in getBoolDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING)"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="辅料入库限制" prop="auxiliaryMaterialInLimit">
        <el-select v-model="formData.auxiliaryMaterialInLimit" placeholder="请选择辅料入库限制">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.BASEORDERCONFIG_ACCESSORIES_MATERIAL_IN_LIMIT)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="毛利分析成本" prop="profitAnalysisBase">
        <el-select v-model="formData.profitAnalysisBase" placeholder="请选择毛利分析成本">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.BASEORDERCONFIG_FINANCE_COST_ANALYSIS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="启用财务结账" prop="enableFinancialClosing">
        <el-radio-group v-model="formData.enableFinancialClosing">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="财务结账日期" prop="financialClosingDate">
        <el-date-picker
          v-model="formData.financialClosingDate"
          type="date"
          value-format="x"
          placeholder="选择财务结账日期"
        />
      </el-form-item>
      <el-form-item label="外协核价规格单位" prop="outsourcingPricingUnit">
        <el-input v-model="formData.outsourcingPricingUnit" placeholder="请输入外协核价规格单位" />
      </el-form-item>
      <el-form-item label="外协采购数量是否取上一道工序的完工数" prop="outsourcingPurchaseQtyUseLastProcess">
        <el-radio-group v-model="formData.outsourcingPurchaseQtyUseLastProcess">
          <el-radio
            v-for="dict in getBoolDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING)"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="现场自动打印成品标签" prop="autoPrintProductLabel">
        <el-radio-group v-model="formData.autoPrintProductLabel">
          <el-radio
            v-for="dict in getBoolDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING)"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="生产前是否维护机台" prop="preProductionMaintenance">
        <el-radio-group v-model="formData.preProductionMaintenance">
          <el-radio
            v-for="dict in getBoolDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING)"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="触摸屏自动领料" prop="touchscreenAutoPick">
        <el-radio-group v-model="formData.touchscreenAutoPick">
          <el-radio
            v-for="dict in getBoolDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING)"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="触摸屏打印自动计算张数" prop="touchscreenAutoPrintCount">
        <el-radio-group v-model="formData.touchscreenAutoPrintCount">
          <el-radio
            v-for="dict in getBoolDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING)"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="触摸屏外协不入库可以看下一道工序" prop="touchscreenOutsourceSkipStorage">
        <el-radio-group v-model="formData.touchscreenOutsourceSkipStorage">
          <el-radio
            v-for="dict in getBoolDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING)"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="送货单产品类型" prop="deliveryOrderProductType">
        <el-input v-model="formData.deliveryOrderProductType" placeholder="请输入送货单产品类型" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { getStrDictOptions, getBoolDictOptions, DICT_TYPE } from '@/utils/dict'
import { BaseOrderConfigApi, BaseOrderConfigVO } from '@/api/erp/baseorderconfig'

/** ERP基础资料-开单参数系统参数 表单 */
defineOptions({ name: 'BaseOrderConfigForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  autoApproveOrder: undefined,
  allowEditApprovedOrder: undefined,
  dataBackupPath: undefined,
  defaultScheduleWarningDays: undefined,
  materialCalculateRule: undefined,
  useDieCutSpecForMaterialCalc: undefined,
  supplierQuotePolicy: undefined,
  minBoardWidth: undefined,
  directSupplierRawMaterial: undefined,
  directSupplierAuxiliaryMaterial: undefined,
  defaultPaperboardDeliveryDays: undefined,
  preventZeroPriceOrder: undefined,
  customerQuotePolicy: undefined,
  defaultDeliveryDaysBox: undefined,
  defaultDeliveryDaysColorBox: undefined,
  autoApproveSalesOrder: undefined,
  allowNonStockDelivery: undefined,
  enableSchedulingDelivery: undefined,
  carOvercapacityWarning: undefined,
  systemMonthEnd: undefined,
  rawMaterialInLimit: undefined,
  productInLimit: undefined,
  productDeliveryHint: undefined,
  defaultProductUnit: undefined,
  productInLimit2: undefined,
  deliveryAutoAudit: undefined,
  productManagementMode: undefined,
  productDeliveryMode: undefined,
  deliveryAutoPickOrProduce: undefined,
  auxiliaryMaterialInLimit: undefined,
  profitAnalysisBase: undefined,
  enableFinancialClosing: undefined,
  financialClosingDate: undefined,
  outsourcingPricingUnit: undefined,
  outsourcingPurchaseQtyUseLastProcess: undefined,
  autoPrintProductLabel: undefined,
  preProductionMaintenance: undefined,
  touchscreenAutoPick: undefined,
  touchscreenAutoPrintCount: undefined,
  touchscreenOutsourceSkipStorage: undefined,
  deliveryOrderProductType: undefined
})
const formRules = reactive({
  autoApproveOrder: [{ required: true, message: '订单保存后自动审核不能为空', trigger: 'blur' }],
  allowEditApprovedOrder: [{ required: true, message: '订单在审核状态下可直接修改不能为空', trigger: 'blur' }],
  useDieCutSpecForMaterialCalc: [{ required: true, message: '是否按模切规格计算材料不能为空', trigger: 'blur' }],
  directSupplierRawMaterial: [{ required: true, message: '原料采购是否按供应商不能为空', trigger: 'blur' }],
  directSupplierAuxiliaryMaterial: [{ required: true, message: '辅料采购是否按供应商不能为空', trigger: 'blur' }],
  preventZeroPriceOrder: [{ required: true, message: '采购单价为0不允许保存不能为空', trigger: 'blur' }],
  autoApproveSalesOrder: [{ required: true, message: '销售订单保存后自动审核不能为空', trigger: 'blur' }],
  allowNonStockDelivery: [{ required: true, message: '不入库可以开送货单不能为空', trigger: 'blur' }],
  enableSchedulingDelivery: [{ required: true, message: '送货需按排程发货不能为空', trigger: 'blur' }],
  carOvercapacityWarning: [{ required: true, message: '送货单车量超容量提醒不能为空', trigger: 'blur' }],
  enableFinancialClosing: [{ required: true, message: '启用财务结账不能为空', trigger: 'blur' }],
  outsourcingPurchaseQtyUseLastProcess: [{ required: true, message: '外协采购数量是否取上一道工序的完工数不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await BaseOrderConfigApi.getBaseOrderConfig()
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as BaseOrderConfigVO
    if (formType.value === 'create') {
      await BaseOrderConfigApi.createBaseOrderConfig(data)
      message.success(t('common.createSuccess'))
    } else {
      await BaseOrderConfigApi.updateBaseOrderConfig(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    autoApproveOrder: undefined,
    allowEditApprovedOrder: undefined,
    dataBackupPath: undefined,
    defaultScheduleWarningDays: undefined,
    materialCalculateRule: undefined,
    useDieCutSpecForMaterialCalc: undefined,
    supplierQuotePolicy: undefined,
    minBoardWidth: undefined,
    directSupplierRawMaterial: undefined,
    directSupplierAuxiliaryMaterial: undefined,
    defaultPaperboardDeliveryDays: undefined,
    preventZeroPriceOrder: undefined,
    customerQuotePolicy: undefined,
    defaultDeliveryDaysBox: undefined,
    defaultDeliveryDaysColorBox: undefined,
    autoApproveSalesOrder: undefined,
    allowNonStockDelivery: undefined,
    enableSchedulingDelivery: undefined,
    carOvercapacityWarning: undefined,
    systemMonthEnd: undefined,
    rawMaterialInLimit: undefined,
    productInLimit: undefined,
    productDeliveryHint: undefined,
    defaultProductUnit: undefined,
    productInLimit2: undefined,
    deliveryAutoAudit: undefined,
    productManagementMode: undefined,
    productDeliveryMode: undefined,
    deliveryAutoPickOrProduce: undefined,
    auxiliaryMaterialInLimit: undefined,
    profitAnalysisBase: undefined,
    enableFinancialClosing: undefined,
    financialClosingDate: undefined,
    outsourcingPricingUnit: undefined,
    outsourcingPurchaseQtyUseLastProcess: undefined,
    autoPrintProductLabel: undefined,
    preProductionMaintenance: undefined,
    touchscreenAutoPick: undefined,
    touchscreenAutoPrintCount: undefined,
    touchscreenOutsourceSkipStorage: undefined,
    deliveryOrderProductType: undefined
  }
  formRef.value?.resetFields()
}
</script>